﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using BusinessRule.System;
public partial class UserDeviceInfo_Item : PageBase
{
    private OrgainzationManager orgManager = new OrgainzationManager();
    private int deviceId = -1;

    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {

            deviceId = int.Parse(Request.QueryString["id"]);
        }
        catch
        {
            deviceId = -1;

        }


        if (!IsPostBack)
        {


            BindUnboundUser();
            if (deviceId == -1)
            {
                ViewState["isAdd"] = true;
            }
            else
            {
                ViewState["isAdd"] = false;
                this.txtImei.Enabled = false;
                BindDeviceItem();
            }

            //绑定页面
            if (this.chb_IsBound.Checked)
            {
                showBoundInfo();
            }

        }
    }

    private void showBoundInfo()
    {

        this.tr_bound1.Visible = true;
        this.tr_bound2.Visible = true;
    }
    private void hideBoundInfo()
    {

        this.tr_bound1.Visible = false;
        this.tr_bound2.Visible = false;
    }

    private void BindUnboundUser()
    {

        ddlUsers.DataSource = new MobileDeviceInfoManager().GetDeviceUnboundUsers(MyStateManager.DomainID);

        ddlUsers.DataTextField = "NAME";
        ddlUsers.DataValueField = "ID";
        ddlUsers.DataBind();
        ddlUsers.Items.Insert(0, new ListItem("--请选择--", "-1"));
    }

    private void BindDeviceItem()
    {

        //设备信息
        MobileDeviceInfo mobile = new MobileDeviceInfoManager().LoadByID(deviceId);
        if (null != mobile)
        {

            this.txtImei.Text = mobile.IMEICode;
            this.txtBrandName.Text = mobile.BrandName;
            this.txtModel.Text = mobile.Model;
            if (null != mobile.ACTIVE)
            {
                this.chb_DeviceActive.Checked = mobile.ACTIVE;
            }


            //设备绑定信息

            UserDeviceBound bound = UserDeviceBoundManager.GetUserDeviceByDeviceID(deviceId);
            if (null != bound)
            {
                this.chb_IsBound.Checked = true;
                this.ddlUsers.Items.RemoveAt(0);
                USERS  user = new UserManager().GetUserByID(bound.UserID);
                this.ddlUsers.Items.Insert(0, new ListItem(user.PERSON.Name + "(" + user.CODE + ")", bound.UserID.ToString()));
                if (null != bound.ACTIVE)
                {
                    this.chb_DeviceBoundActive.Checked = bound.ACTIVE;
                }
            }
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        //判断IMEICode有无重复
        if ((Boolean)ViewState["isAdd"])
        {
            MobileDeviceInfo tempMobile = new MobileDeviceInfoManager().LoadByCode(this.txtImei.Text.Trim());
            if (null != tempMobile)
            {

                ShowMessage("IMEICode重复，该设备已经注册过");
                return;
            }
        }
        MobileDeviceInfo mobile = new MobileDeviceInfo();
        long time = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmss"));
        string lastUpdater = new UserManager().GetUserByID(MyStateManager.UserID).CODE;
        if (!(Boolean)ViewState["isAdd"])
        {
            mobile = new MobileDeviceInfoManager().LoadByID(deviceId);
        }
        else
        {
            mobile.CREATE_TIME = time;
        }
        mobile.IMEICode = this.txtImei.Text;
        mobile.BrandName = this.txtBrandName.Text;
        mobile.Model = this.txtModel.Text;
        mobile.ACTIVE = this.chb_DeviceActive.Checked ? true : false;
        mobile.DOMAIN_ID = MyStateManager.DomainID;
        mobile.LASTUP_DATER = lastUpdater;
        mobile.LAST_UPDATE_TIME = time;
        mobile.Save();
        mobile = new MobileDeviceInfoManager().LoadByCode(mobile.IMEICode);
        UserDeviceBound bound = UserDeviceBoundManager.GetUserDeviceByDeviceID(mobile.ID);
        //更新或保存绑定关系
        if (this.chb_IsBound.Checked)
        {
            if (this.ddlUsers.SelectedValue == "-1")
            {
                ShowMessage("请选择人员");
                return;
            }
            if (null == bound)
            {
                bound = new UserDeviceBound();
                bound.CREATE_TIME = time;
            }
            bound.UserID = Decimal.Parse(this.ddlUsers.SelectedValue);
            bound.DeviceID = mobile.ID;
            bound.ACTIVE = this.chb_DeviceBoundActive.Checked ? true : false;
            bound.DOMAIN_ID = MyStateManager.DomainID;
            bound.LAST_UPDATER = lastUpdater;
            bound.LAST_UPDATE_TIME = time;
            bound.Save();
        }
        //否则删除
        else
        {
            if (null != bound)
            {
                bound.Delete();
            }
        }
        Response.Redirect("UserDeviceInfo_List.aspx");
    }
    protected void chb_IsBound_CheckedChanged(object sender, EventArgs e)
    {
        if (this.chb_IsBound.Checked)
            showBoundInfo();
        else
            hideBoundInfo();
    }
}

